package com.leen.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.leen.admin.dto.AdminDTO;
import com.leen.admin.dto.AdminLoginDTO;
import com.leen.admin.entity.Admin;
import com.leen.admin.service.AdminService;
import com.leen.common.vo.ResponseVO;
import com.leen.common.vo.ResultEnum;
import com.leen.common.vo.ResultVO;
import com.leen.news.feign.INewsFeignClient;
import com.leen.security.CurrentAdminInfo;
import com.leen.security.domain.AdminLoginUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(value = "/admin")
public class AdminController  {

    @Autowired
    private AdminService  adminService;


    @GetMapping(value = "/findByAccount")
    public  ResultVO<String> findByAccount(@RequestParam(value = "account") String account){
        AdminLoginUserDetails admin=this.adminService.findByAccount(account);
        if(admin==null){
            return ResultVO.failed(ResultEnum.USER_NOT_EXIST_ERROR);
        }

        String str=JSONObject.toJSONString(admin);
        return ResultVO.success(str);
    }


    /**
     * 后台登录实现
     * @return
     */
    @PostMapping(value = "/login")
    public ResultVO loginCheck(@RequestBody AdminLoginDTO adminLoginDTO,
                               @RequestHeader(value = "key",defaultValue = "") String key  ,
                               HttpServletRequest request){
        //验证逻辑
        adminLoginDTO.setKey(key);

        return this.adminService.login(adminLoginDTO,request);

    }

    /**
     * 获取登录的用户信息
     * @return
     */
    @GetMapping(value = "/info")
    public ResultVO loginCheck(){

        AdminLoginUserDetails adminLoginUserDetails = CurrentAdminInfo.getAdminLoginUserDetails();

        return ResultVO.success(adminLoginUserDetails);

    }

    @Autowired
    private INewsFeignClient  iNewsFeignClient;

    @GetMapping(value = "/search")
    public ResultVO data(){
        System.out.println("通过feign去拉取 news-servcie中的服务");

        return  iNewsFeignClient.getNewsData();
    }


    @PostMapping(value = "/add")
    private ResultVO adminAdd(@RequestBody Admin admin){
        Object save = this.adminService.save(admin);
        if(save!=null){
            return ResultVO.success(ResultEnum.DB_SUCCESS);
        }else {
            return ResultVO.success(ResultEnum.DB_FAILED);
        }

    }

    @PostMapping(value = "/updata")
    private ResultVO updata(Admin admin){
        Object save = this.adminService.save(admin);
        if(save!=null){
            return ResultVO.success(ResultEnum.DB_SUCCESS);
        }else {
            return ResultVO.success(ResultEnum.DB_FAILED);
        }

    }

    /**
     * 分页查询
     * @param adminDTO
     * @return
     */
    @GetMapping(value = "/page/search")
    public ResultVO<ResponseVO> searchPage(AdminDTO adminDTO){
        ResponseVO search = this.adminService.serach(adminDTO);
        return ResultVO.success(search) ;
    }

}
